package com.titan.core.base;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;

import com.titan.core.utils.db.DBUtils;



/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月20日
 */
public abstract class BaseRowMapper<T> implements RowMapper<T> {
	private static final String UNUSED = "unused";
	private static final ThreadLocal<Map<String,Integer>> cacheMetaData = new ThreadLocal<Map<String,Integer>>();
	
	@SuppressWarnings(UNUSED)
	public static void clear() {
		Map<String,Integer> container = cacheMetaData.get();
		if(container!=null) {
			container.clear();
			container = null;
		}
		cacheMetaData.set(null);
	}
	
	@SuppressWarnings(UNUSED)
	private static int get(ResultSet rs, String columnName) throws SQLException {
		Map<String,Integer> container = cacheMetaData.get();
        if(container==null) {
            container =new HashMap<String,Integer>();
            ResultSetMetaData meta = rs.getMetaData();
            int count = meta.getColumnCount();
            for (int i = 1; i <= count; i++) {
            	String name = meta.getColumnName(i);
            	container.put(name.toLowerCase(), i);
			}
            cacheMetaData.set(container);
        }
        Integer value = container.get(columnName.toLowerCase());
        if(value==null) {
        	return -1;
        }
        return value;
	}
	
	protected static Date getTimestamp(ResultSet rs, String name, Date defaultValue) throws SQLException {
		int columnIndex = get(rs, name);
		if(columnIndex <= 0) {
			return defaultValue;
		}
		return rs.getTimestamp(columnIndex);
	}
	protected static String getString(ResultSet rs, String name, String defaultValue) throws SQLException {
		int columnIndex = get(rs, name);
		if(columnIndex <= 0) {
			return defaultValue;
		}
		return rs.getString(columnIndex);
	}	
	protected static int getInt(ResultSet rs, String name, int defaultValue) throws SQLException {
		int columnIndex = get(rs, name);
		if(columnIndex <= 0) {
			return defaultValue;
		}
		return rs.getInt(columnIndex);
	}	
	protected static long getLong(ResultSet rs, String name, long defaultValue) throws SQLException {
		int columnIndex = get(rs, name);
		if(columnIndex <= 0) {
			return defaultValue;
		}
		return rs.getLong(columnIndex);
	}	
	protected static double getDouble(ResultSet rs, String name, double defaultValue) throws SQLException {
		int columnIndex = get(rs, name);
		if(columnIndex == -1) {
			return defaultValue;
		}
		return rs.getDouble(columnIndex);
	}	
    protected static float getFloat(ResultSet rs, String name, float defaultValue) throws SQLException {
        int columnIndex = get(rs, name);
        if(columnIndex == -1) {
            return defaultValue;
        }
        return rs.getFloat(columnIndex);
    }   
	protected static byte[] getBytes(ResultSet rs, String name, byte[] defaultValue) throws SQLException {
		int columnIndex = get(rs, name);
		if(columnIndex == -1) {
			return defaultValue;
		}
		return DBUtils.getBlobAsBytes(rs, columnIndex);
//		return rs.getDouble(columnIndex);
	}
	
    protected static void mapBaseDomain(ResultSet from, BaseDomain to) throws SQLException {
//    	to.setId(getInt(from, "id", 0));
//    	to.setCreate_dt(getTimestamp(from, "create_dt", null));
//		to.setCreate_name(getString(from, "create_name", null));
//		to.setUpdate_dt(getTimestamp(from, "update_dt", null));
//		to.setUpdate_name(getString(from, "update_name", null));
//		
//		to.setClose_dt(getTimestamp(from, "close_dt", null));
//		to.setClose_name(getString(from, "close_name", null));
//		to.setClose_flag(getInt(from, "close_flag", 0));
    }
}
